home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-02-26 | 17.5 KB | 705 lines | [TEXT/KAHL] |
- //--------------------------------------------------------------------------
- //
- // MyMultipleMovieApp.
- // by John Wang
- //
- // Description: QuickTime Multiple Movie Application.
- //
- // Version: 1.0 11/09/93 Completed for develop column.
- //
- //
- //--------------------------------------------------------------------------
-
- // #includes:
-
- #include <GestaltEqu.h>
- #include <Script.h>
- #include <ToolUtils.h>
-
- #include "MyApplication.h"
- #include "BetterFlattenMovie.h"
-
- //--------------------------------------------------------------------------
-
- // Globals:
-
- // These globals are used by the shell and must be defined:
-
- long gQDfeature, gWindowCount;
- Str255 gMyAboutTitle = "\PQuickTime Application";
- Str255 gMyAboutDesc = "\PThis is a multiple movie playback app.";
-
- struct WindowInfo {
- // Document info.
- FSSpec theFSSpec; // FSSpec for document.
- short refNum; // refNum for document.
-
- // Current movie playing info.
- Movie theMovie; // Current movie. nil if none.
- MovieController theMC; // Current movie controller. nil if none.
- Str255 moviename; // Name of movie if exists.
- short resId; // resource id of movie in document. -1 if no resId.
- long movieOffset; // offset to movie resource atom in data fork. -1 if no offset.
- };
- typedef struct WindowInfo WindowInfo, *WindowInfoPtr, **WindowInfoHandle;
-
- //--------------------------------------------------------------------------
-
- // MyInitialize:
-
- // is called at init time after the toolbox is initialized. This routine is
- // called only once.
-
- OSErr MyInitialize()
- {
- OSErr err;
-
- // We require QuickTime so make sure it is available.
- if (err = Gestalt(gestaltQuickTime, &gQDfeature))
- return(err);
-
- if (err = EnterMovies())
- return(err);
-
- return(noErr);
- }
-
- //--------------------------------------------------------------------------
-
- // MyEvent:
-
- // This routine is called for each window for an event.
-
- Boolean MyEvent(WindowPtr theWindow, EventRecord *myEvent)
- {
- WindowInfoHandle myWinfo;
- Boolean ret;
-
- ret = FALSE;
- myWinfo = (WindowInfoHandle) GetWRefCon(theWindow);
- if ((**myWinfo).theMC != nil) {
- ret = MCIsPlayerEvent((**myWinfo).theMC, myEvent);
- }
-
- return(ret);
- }
-
- //--------------------------------------------------------------------------
-
- // MyIdle:
-
- // Depending on the value of the constant MYIDLEDEF, this routine may have different
- // calling frequency.
- // If MYIDLEDEF == 2, then this routine gets called for every
- // window owned by the application of type MAS_WINDOWDOC per iteration of
- // the event loop. You can assume that the port and gdevice are set to the window.
- // If MYIDLEDEF == 1, then this routine gets called once per event loop. theWindow will be nil.
- // If MYIDLEDEF == 0, this routine never gets called.
-
- void MyIdle(WindowPtr theWindow)
- {
- WindowInfoHandle myWinfo;
-
- myWinfo = (WindowInfoHandle) GetWRefCon(theWindow);
- if ((**myWinfo).theMC != nil) {
- MCIdle((**myWinfo).theMC);
- }
- }
-
- //--------------------------------------------------------------------------
-
- // MyDraw:
-
- // is called when redrawing the window is needed. The port and gdevice
- // is already set and begin and end update are called for MyDraw.
-
- void MyDraw(WindowPtr theWindow)
- {
- // Erase a black background for movie.
- PaintRect(&(theWindow->portRect));
- }
-
- //--------------------------------------------------------------------------
-
- // MyFinishup:
-
- // This routine gets called once when the application is quitting.
- // This gives the app the chance to clean up.
-
- void MyFinishup()
- {
- // As discussed in issue #13 of 'develop' magazine, it is better to let QuickTime
- // call ExitMovies().
- }
-
- //--------------------------------------------------------------------------
-
- // MyYieldTime:
-
- // This routine is called whenever the app receives a suspend or resume event. This
- // allows the yield time (passed to WaitNextEvent) to be changed. I.e., if you don't
- // do much processing the in background, you should set the yield to a high value
- // when the suspend message is received.
-
- long MyYieldTime(long message)
- {
- if (message)
- // Resume message
- return(0);
- else
- // Suspend message
- return(30);
- }
-
- //--------------------------------------------------------------------------
-
- // MyDoCommand:
-
- // If the shell's doCommand routine can not process the menu selection, then this routine is
- // called. Do not call HiliteMenu because it is called by the shell.
- // If the command can not be handled, this it must be an error and the shell will
- // present a FATAL error to the user.
-
- OSErr MyDoCommand(short theMenu, short theItem)
- {
- WindowPtr frontWindow;
- WindowInfoHandle myWinfo;
-
- switch (theMenu) {
- case MENU_COLLECTION:
- if (IsMyWindow(frontWindow = FrontWindow())) {
- myWinfo = (WindowInfoHandle) GetWRefCon(frontWindow);
- switch (theItem) {
- case MENU_MACADDRESATOM:
- Mac_AddMovieResAtom(&((**myWinfo).refNum), &((**myWinfo).theFSSpec));
- break;
- case MENU_MACADDALL:
- Mac_AddMovieAll(&((**myWinfo).refNum), &((**myWinfo).theFSSpec));
- break;
- case MENU_CROSSADDALL:
- Cross_AddMovieAll(&((**myWinfo).refNum), &((**myWinfo).theFSSpec));
- break;
- case MENU_BOTHADDALL:
- Both_AddMovieAll(&((**myWinfo).refNum), &((**myWinfo).theFSSpec));
- break;
- default:
- return(theItem);
- }
- adjustMoviesMenu(frontWindow);
- } else
- SysBeep(50);
- break;
-
- case MENU_MOVIES:
- if (IsMyWindow(frontWindow = FrontWindow()))
- SelectThisMovie(theItem);
- else
- SysBeep(50);
- break;
-
- default:
- return(theItem);
- }
-
- return(noErr);
- }
-
- //--------------------------------------------------------------------------
-
- // MyDoKeyDown:
-
- void MyDoKeyDown(EventRecord *myEvent)
- {
- }
-
- //--------------------------------------------------------------------------
-
- // If the shell receives a inContent mouse down, or zoom window mouse down,
- // the following routines may be called (MyInContent, MyActivateEvent, MyZoomWindow, MyAdjustMenus) :
-
- void MyInContent(WindowPtr foundWindow, Point where)
- {
- }
-
- void MyZoomWindow(WindowPtr foundWindow, Boolean zoomOut)
- {
- }
-
- void MyAdjustMenus()
- {
- }
-
- //--------------------------------------------------------------------------
-
- // MyNew and MyOpen:
-
- WindowPtr createMoviesWindow(FSSpec *movieFSSpec, short theRefNum);
- WindowPtr createMoviesWindow(FSSpec *movieFSSpec, short theRefNum)
- {
- WindowPtr myWindow;
- WindowInfoHandle myWinfo;
- Rect windowRect;
- GDHandle myMaxDevice;
-
- // Create window.
- myMaxDevice = GetMaxDevice(&((**GrayRgn).rgnBBox));
- windowRect = (**((**myMaxDevice).gdPMap)).bounds;
- myWindow = NewCWindow(0L, &windowRect, (*movieFSSpec).name, 1, noGrowDocProc, (WindowPtr) -1, TRUE, 0L);
- OffsetRect(&windowRect, -windowRect.left, -windowRect.top);
- SetMyWindow(myWindow);
- SetGWorld((CGrafPtr) myWindow, GetMainDevice());
-
- // This handle must be locked because we dereference it all the time.
- myWinfo = (WindowInfoHandle) NewHandle(sizeof(WindowInfo));
- MoveHHi((Handle) myWinfo);
- HLock((Handle) myWinfo);
- SetWRefCon(myWindow, (long) myWinfo);
-
- (**myWinfo).theFSSpec = *movieFSSpec;
- (**myWinfo).refNum = theRefNum;
- (**myWinfo).theMovie = nil;
- (**myWinfo).theMC = nil;
- (**myWinfo).moviename[0] = 0;
- (**myWinfo).resId = -1;
- (**myWinfo).movieOffset = -1;
-
- return(myWindow);
- }
-
- void MyNew()
- {
- WindowPtr myWindow;
- StandardFileReply reply;
- short myRefNum;
- MenuHandle myMenu;
-
- if (gWindowCount > 0) {
- SysBeep(50);
- return;
- }
-
- // Ask for a new document file. If none given, then quit.
- StandardPutFile("\PNew document", "\PMovie Collection", &reply);
- if (!reply.sfGood)
- return;
-
- // Use QuickTime to create movie file.
- if (CreateMovieFile(&reply.sfFile, 'WnG1', smSystemScript,
- createMovieFileDeleteCurFile | createMovieFileDontCreateMovie, &myRefNum, nil)) {
- Alert(ALERT_CANTNEW, nil);
- return;
- }
-
- myWindow = createMoviesWindow(&(reply.sfFile), myRefNum);
- gWindowCount += 1;
-
- // Add new menu.
- adjustMoviesMenu(myWindow);
- }
-
- // If theFSS is not nil, then the file to be opened is specified in the FSSpec.
- void MyOpen(FSSpec *theFSS)
- {
- OSErr err;
- WindowPtr myWindow;
- StandardFileReply reply;
- FSSpec myFSSpec;
- SFTypeList types;
- short myRefNum;
- MenuHandle myMenu;
-
- if (gWindowCount > 0) {
- SysBeep(50);
- return;
- }
-
- // If theFSS was nil, then open a file manually.
- if (theFSS == nil) {
- types[0] = 'MooV';
- StandardGetFilePreview(nil, 1, types, &reply);
- if (!reply.sfGood)
- return;
- myFSSpec = reply.sfFile;
- } else {
- myFSSpec = *theFSS;
- }
-
- // Open movie file.
- err = OpenMovieFile(&myFSSpec, &myRefNum, fsRdWrPerm);
- if (myRefNum == -1 || err) {
- Alert(ALERT_ERROROPEN, nil);
- return;
- }
-
- myWindow = createMoviesWindow(&myFSSpec, myRefNum);
- gWindowCount += 1;
-
- // Add new menu.
- adjustMoviesMenu(myWindow);
- }
-
- //--------------------------------------------------------------------------
-
- // MyClose:
-
- void MyClose()
- {
- WindowPtr closeWindow;
- WindowInfoHandle myWinfo;
- MenuHandle myMenu;
-
- if ((closeWindow = FrontWindow()) == nil)
- return;
-
- if (IsMyWindow(closeWindow)) {
- myWinfo = (WindowInfoHandle) GetWRefCon(closeWindow);
-
- if ((**myWinfo).theMC != nil)
- DisposeMovieController((**myWinfo).theMC);
- if ((**myWinfo).theMovie != nil);
- DisposeMovie((**myWinfo).theMovie);
- if ((**myWinfo).refNum != -1)
- CloseMovieFile((**myWinfo).refNum);
- DisposHandle((Handle) myWinfo);
- DisposeWindow(closeWindow);
- gWindowCount -= 1;
-
- // Delete old menu.
- adjustMoviesMenu(nil);
- CompactMem(0xfffffff);
- }
- }
-
- //--------------------------------------------------------------------------
-
- // MySave, MySaveAs, MyPageSetup, MyPrint, MyUndo, MyCut, MyCopy,
- // MyPaste, MyClear, and MySelectAll: are not implemented.
-
- void MySave()
- {
- SysBeep(50);
- }
-
- void MySaveAs()
- {
- SysBeep(50);
- }
-
- void MyPageSetup()
- {
- SysBeep(50);
- }
-
- void MyPrint()
- {
- SysBeep(50);
- }
-
- void MyUndo(void)
- {
- SysBeep(50);
- }
-
- void MyCut(void)
- {
- SysBeep(50);
- }
-
- void MyCopy(void)
- {
- SysBeep(50);
- }
-
- void MyPaste(void)
- {
- SysBeep(50);
- }
-
- void MyClear(void)
- {
- SysBeep(50);
- }
-
- void MySelectAll(void)
- {
- }
-
-
- //--------------------------------------------------------------------------
-
- // Utility routines (IsMyWindow, SetMyWindow, IsMyClipWindow, SetMyClipWindow):
-
- // Called to determine if the window is owned by app.
- Boolean IsMyWindow(WindowPtr theWindow)
- {
- if (theWindow)
- return(((CWindowPeek) theWindow)->windowKind == MAS_WINDOWDOC);
- else
- return(FALSE);
- }
-
- // Called to set the window's creator.
- void SetMyWindow(WindowPtr theWindow)
- {
- if (theWindow)
- ((CWindowPeek) theWindow)->windowKind = MAS_WINDOWDOC;
- }
-
- // Called to determine if window is a show clipboard window.
- Boolean IsMyClipWindow(WindowPtr theWindow)
- {
- if (theWindow)
- return(((CWindowPeek) theWindow)->windowKind == MAS_WINDOWCLIP);
- else
- return(FALSE);
- }
-
- // Called to set the creator as the window clip.
- void SetMyClipWindow(WindowPtr theWindow)
- {
- if (theWindow)
- ((CWindowPeek) theWindow)->windowKind = MAS_WINDOWCLIP;
- }
-
- //--------------------------------------------------------------------------
-
- void adjustMoviesMenu(WindowPtr theWindow)
- {
- WindowInfoHandle myWinfo;
- FSSpec theFSSpec;
- short theRefNum;
- MenuHandle myMenu;
- short movieCount;
- Handle movieResource;
- short movieID;
- ResType movieType;
- Str255 movieName;
- short i;
-
- // Delete old menu if it exists.
- if (myMenu = GetMHandle(MENU_MOVIES)) {
- DeleteMenu(MENU_MOVIES);
- DisposeMenu(myMenu);
- }
-
- // If a window is passed, then update the menu for it.
- if (IsMyWindow(theWindow)) {
- myWinfo = (WindowInfoHandle) GetWRefCon(theWindow);
- theFSSpec = (**myWinfo).theFSSpec;
- theRefNum = (**myWinfo).refNum;
- myMenu = NewMenu(MENU_MOVIES, theFSSpec.name);
- InsertMenu(myMenu, 0);
-
- // Add resource fork movies into menu.
- if (theRefNum != -1) {
- movieCount = Count1Resources('moov');
- for (i=1; i<=movieCount; i++) {
- movieResource = Get1IndResource('moov', i);
- GetResInfo(movieResource, &movieID, &movieType, movieName);
- if (movieName[0] == 0) {
- NumToString(movieID, &movieName[23]);
- movieName[0] = movieName[23] + 23;
- BlockMove("Resource Fork Movie ID#", &movieName[1], 23);
- }
- AppendMenu(myMenu, movieName);
- ReleaseResource(movieResource);
- }
- }
-
- // Add single fork movies into menu.
- if (CountMoviesInDataFork(&theFSSpec, &movieCount) == noErr) {
- for (i=0; i<movieCount; i++) {
- NumToString(i+1, &movieName[19]);
- movieName[0] = movieName[19] + 19;
- BlockMove("Single Fork Movie #", &movieName[1], 19);
- AppendMenu(myMenu, movieName);
- }
- }
- }
-
- // Update MenuBar.
- DrawMenuBar();
- }
-
- void SelectThisMovie(short item)
- {
- OSErr err;
- WindowPtr frontWindow;
- WindowInfoHandle myWinfo;
- short resCount, dataCount;
- Handle movieResource;
- short movieID;
- ResType movieType;
- long controllerFlags;
- Rect movieBounds;
- short i, xoffset, yoffset;
- Str255 junkStr;
- short myRefNum;
- Movie tempMovie;
- short tempResId;
- long tempMovieOffset;
-
- if ((frontWindow = FrontWindow()) == nil)
- return;
-
- if (IsMyWindow(frontWindow)) {
- myWinfo = (WindowInfoHandle) GetWRefCon(frontWindow);
-
- // Get rid of old movie
- if ((**myWinfo).theMC != nil)
- DisposeMovieController((**myWinfo).theMC);
- if ((**myWinfo).theMovie != nil)
- DisposeMovie((**myWinfo).theMovie);
- (**myWinfo).theMovie = nil;
- (**myWinfo).theMC = nil;
- (**myWinfo).moviename[0] = 0;
- (**myWinfo).resId = -1;
- (**myWinfo).movieOffset = -1;
-
- // Invalidate window.
- InvalRect(&(frontWindow->portRect));
-
- // Count the number of movies in resource fork and data fork
- resCount = Count1Resources('moov');
- CountMoviesInDataFork(&((**myWinfo).theFSSpec), &dataCount);
-
- // Find which movie to play.
- if (item <= resCount) {
- movieResource = Get1IndResource('moov', item);
- GetResInfo(movieResource, &tempResId, &movieType, (**myWinfo).moviename);
- ReleaseResource(movieResource);
- err = NewMovieFromFile(&tempMovie, (**myWinfo).refNum, &tempResId, nil,
- newMovieActive, (Boolean *) 0);
- (**myWinfo).theMovie = tempMovie;
- (**myWinfo).resId = tempResId;
- if (err) {
- Alert(ALERT_CANTOPEN, nil);
- return;
- }
- } else {
- item = item-resCount;
- err = SearchMoviesInDataFork(&((**myWinfo).theFSSpec), item, &tempMovieOffset);
- (**myWinfo).movieOffset = tempMovieOffset;
- if (err) {
- Alert(ALERT_CANTOPEN, nil);
- return;
- }
- if (FSpOpenDF(&((**myWinfo).theFSSpec), fsRdPerm, &myRefNum) == noErr) {
- err = NewMovieFromDataFork(&tempMovie, myRefNum, (**myWinfo).movieOffset, newMovieActive, nil);
- (**myWinfo).theMovie = tempMovie;
- FSClose(myRefNum);
- if (err) {
- Alert(ALERT_CANTOPEN, nil);
- return;
- }
- }
- }
-
- // Set the movie box into middle of window.
- SetMovieGWorld((**myWinfo).theMovie, (CGrafPtr) frontWindow, 0);
- GetMovieBox((**myWinfo).theMovie, &movieBounds);
- OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
- SetMovieBox((**myWinfo).theMovie, &movieBounds);
- xoffset = (frontWindow->portRect.right - frontWindow->portRect.left
- - movieBounds.right + movieBounds.left) / 2;
- yoffset = (frontWindow->portRect.bottom - frontWindow->portRect.top
- - movieBounds.bottom + movieBounds.top) / 2;
- OffsetRect(&movieBounds, xoffset, yoffset);
- (**myWinfo).theMC = NewMovieController((**myWinfo).theMovie, &movieBounds, mcTopLeftMovie);
-
- // Tell the controller to attach a movie’s CLUT to the window as appropriate.
- MCDoAction((**myWinfo).theMC, mcActionGetFlags, &controllerFlags);
- MCDoAction((**myWinfo).theMC, mcActionSetFlags, (void *)(controllerFlags | mcFlagsUseWindowPalette));
-
- // Allow the controller to accept keyboard events.
- MCDoAction((**myWinfo).theMC, mcActionSetKeysEnabled, (void *)true);
- }
- }
-
- //--------------------------------------------------------------------------
-
- void getNewMovie(Movie *theMovie, Str255 *movieName)
- {
- StandardFileReply reply;
- SFTypeList types;
- short refNum, resId;
-
- *theMovie = nil;
-
- types[0] = 'MooV';
- StandardGetFilePreview(nil, 1, types, &reply);
- if (!reply.sfGood) return;
-
- // Read movie atom of source movie.
- if (OpenMovieFile(&reply.sfFile, &refNum, fsRdPerm)) {
- SysBeep(50);
- return;
- }
- resId = 0;
- (*movieName)[0] = 0;
- if (NewMovieFromFile(theMovie, refNum, &resId, *movieName, 0, (Boolean *) 0)) {
- SysBeep(50);
- *theMovie = nil;
- return;
- }
- if ((*movieName)[0] == 0)
- BlockMove(reply.sfFile.name, *movieName, reply.sfFile.name[0]+1);
- CloseMovieFile(refNum);
- }
-
- void Mac_AddMovieResAtom(short *docRefNum, FSSpec *docFSSpec)
- {
- Movie theMovie;
- Str255 movieName;
- short resId;
-
- getNewMovie(&theMovie, &movieName);
- if (theMovie != nil) {
- resId = 0;
- AddMovieResource(theMovie, *docRefNum, &resId, movieName);
- DisposeMovie(theMovie);
- }
- }
-
- void Mac_AddMovieAll(short *docRefNum, FSSpec *docFSSpec)
- {
- Movie theMovie;
- Str255 movieName;
-
- getNewMovie(&theMovie, &movieName);
- if (theMovie != nil) {
- CloseMovieFile(*docRefNum);
- BetterFlattenMovie(theMovie, 0, docFSSpec, 'WnG1', smSystemScript, 0, nil, movieName);
- OpenMovieFile(docFSSpec, docRefNum, fsRdWrPerm);
- DisposeMovie(theMovie);
- }
- }
-
- void Cross_AddMovieAll(short *docRefNum, FSSpec *docFSSpec)
- {
- Movie theMovie;
- Str255 movieName;
-
- getNewMovie(&theMovie, &movieName);
- if (theMovie != nil) {
- CloseMovieFile(*docRefNum);
- BetterFlattenMovieData(theMovie, flattenAddMovieToDataFork, docFSSpec, 'WnG1', smSystemScript, 0);
- OpenMovieFile(docFSSpec, docRefNum, fsRdWrPerm);
- DisposeMovie(theMovie);
- }
- }
-
- void Both_AddMovieAll(short *docRefNum, FSSpec *docFSSpec)
- {
- Movie theMovie;
- Str255 movieName;
-
- getNewMovie(&theMovie, &movieName);
- if (theMovie != nil) {
- CloseMovieFile(*docRefNum);
- BetterFlattenMovie(theMovie, flattenAddMovieToDataFork, docFSSpec, 'WnG1', smSystemScript, 0, nil, movieName);
- OpenMovieFile(docFSSpec, docRefNum, fsRdWrPerm);
- DisposeMovie(theMovie);
- }
- }
-